package secret.hehe.com.secret.Activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import secret.hehe.com.secret.R;
import static secret.hehe.com.secret.Util.Util.*;

/**
 * Created by DELL on 2018/2/28.
 */

public class SearchMailActivity extends AppCompatActivity {

    private String webUrl="file:///android_asset/html/SearchMail.html";
    private WebView webView;
    private String result="";
    private String userId;
    private int isMail=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchmail);

        Intent intent = getIntent();
        try {
            //接受数据
            userId=(String) intent.getSerializableExtra("userId");
            isMail=(int) intent.getSerializableExtra("isMail");
            if(userId==null)userId="";
            //防止变量杀死
            if (savedInstanceState != null) {
                userId = savedInstanceState.getString("userId");
                isMail = savedInstanceState.getInt("isMail");
            }
            if(userId.equals("")){
                //登录页面换人
                result="{\"data\":"+doSelect("user","id!=''",getApplicationContext())+"}";
                this.setTitle("选择账号");
                webUrl="file:///android_asset/html/SearchUser.html";
            }else{
                if(isMail==1){
                    //搜索邮件
                    String where = "recvid='"+userId+"' and isdel=0  order by sendtime desc";
                    result="{\"data\":"+doSelect("message",where,getApplicationContext())+"}";
                    this.setTitle("查找邮件");
                    webUrl="file:///android_asset/html/SearchMail.html";
                }else if(isMail==2){
                    //收藏夹
                    String where = "e.recvid=c.userid and e.eid=c.eid and e.recvid='"+userId+"' order by sendtime desc";
                    result="{\"data\":"+doSelect("message e,collection c",where,getApplicationContext())+"}";
                    this.setTitle("收藏夹");
                    webUrl="file:///android_asset/html/SearchMail.html";
                }else{
                    //选择联系人
                    String where = "a.user1='"+userId+"' and a.user2=u.id and a.isscreen=0";
                    result="{\"data\":"+doSelect("user u,address a",where,getApplicationContext())+"}";
                    this.setTitle("添加收件人");
                    webUrl="file:///android_asset/html/SearchUser.html";
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }


        webView=new WebView(getApplicationContext());
        RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.webview_container);
        startWebView(webView,webUrl,relativeLayout);
        webView.addJavascriptInterface(this, "android");

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }


        Handler x = new Handler();
        x.postDelayed(new loadAllMessage(), 300);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //保存变量
        super.onSaveInstanceState(outState);
        outState.putString("userId", userId);
        outState.putInt("isMail", isMail);
    }

    @Override
    protected void onDestroy(){
        destroyWebView(webView);
        super.onDestroy();
    }
    @Override
    public void finish(){
        Intent intent = new Intent();
        if(isMail==0){

        }else {
            setResult(1002,intent);
        }
        super.finish();
    }

    public void loadMessage(String json) {         //调用js切换内容
        webView.loadUrl("javascript:window.refreshMail("+json+")");
    }
    public void collection(int isCollection) {         //调用js切换内容
        webView.loadUrl("javascript:window.collection("+isCollection+")");
    }

    @JavascriptInterface
    public void toMessage(String eid) {//查看邮件
        try {
            Intent intent = new Intent(SearchMailActivity.this, MessageActivity.class);
            intent.putExtra("eid",eid);
            intent.putExtra("userId",userId);
            intent.putExtra("from",0);
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }
    @JavascriptInterface
    public void toBack(String userId,String icon) {//返回选择的用户
        Intent intent = new Intent();
        intent.putExtra("userId",userId);
        intent.putExtra("icon",Integer.valueOf(icon).intValue());
        setResult(1000, intent);
        this.finish();
    }
    @JavascriptInterface
    public String getUserId(){
        return userId;
    }

    @JavascriptInterface
    public String getIcon(String Icon){
        int icon = Integer.valueOf(Icon).intValue();
        String num="";
        if(icon<10)num="0";
        String url="file:///android_res/drawable/icon"+num+icon;
        return url;
    }

    class loadAllMessage implements Runnable {
        public void run() {
            if(isMail!=0){
                collection(isMail);
            }
            loadMessage(result);
        }

    }
}
